package cn.tedu.straw.gateway.security;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //登录页面
        String loginPageUrl = "/login.html";
        //处理登录的URL,即登录的表单提交到那里去
        String loginProcessingUrl = "/login";
        //不需要登录就可以访问的URL
        String[] urls = {
          loginPageUrl,
          loginProcessingUrl,
          "/favicon.ico",
          "/browser_components/**",
          "/css/**",
          "/img/**",
          "/js/**",
          "/npm/**",
          "/register.html",
          "/straw-api-user/v1/users/reg/student"
        };
        //授权访问
        http.authorizeRequests()
                .antMatchers(urls).permitAll()
                .antMatchers("/test/user/add").hasAnyAuthority("创建用户","系统管理员")
                .antMatchers("/test/user/delete").hasAnyAuthority("删除用户","系统管理员")
//                .antMatchers("/test/user/update").hasAnyAuthority("/web/index","/question/upload")
                .antMatchers("/test/user/list").hasAnyAuthority("查看用户","系统管理员")
                //所有请求都是需要进行验证的
                .anyRequest().authenticated();
        http.formLogin()
                .loginPage(loginPageUrl)
                .loginProcessingUrl(loginProcessingUrl);
        http.csrf().disable();

    }
}
